package com.say.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.say.framework.core.util.SystemResourceLocator;

@Component
public class DataInterceptor extends HandlerInterceptorAdapter {
	
	// Controller 前执行
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setCharacterEncoding("UTF8"); // 设置输出编码
        response.setContentType("text/html;charset=UTF-8");
        String url = request.getServletPath() + (request.getPathInfo() == null ? "" : request.getPathInfo());
        if (url.startsWith("/static/")) { 
        	return true; 
        }
        return true;
    }
    
    //运行Controller后运行
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        request.setCharacterEncoding("UTF-8");
        if (modelAndView != null) {
            initModelAndView(request, modelAndView);
        }
    }
    
    private  void initModelAndView(HttpServletRequest request, ModelAndView modelAndView) throws Exception {
        modelAndView.addObject("sysUrl", SystemResourceLocator.getValue("SYS_URL"));
        modelAndView.addObject("imgUrl", SystemResourceLocator.getValue("IMG_URL"));

    }


}
